

 $(document).ready(function() {
	//$("#cnic").mask("99999-9999999-9");
 });


function validateCaptcha()
{
	var captcha = jQuery.trim( $('#captcha').val() );
	if( captcha == '' )
	{
		return '';	
	}
	else
	if( captcha.length < 5 )
	{
		return '';
	}
	
	
	  var captcha = $('#captcha').val();
		 	
		 $.post(
     	 baseurl+"/trivia.php", 
     	 {action: "checkCaptcha", captcha : captcha }, 
     	 function(data)
		 {
			if( data == 1)
			{
			  $('#valid_captcha').val(1);
			  $('#captcha').removeClass('alert');
			  $('#captcha_msg').hide();

			}
			else
			{
				$('#valid_captcha').val(0);
				$('#captcha').addClass('alert');
				$('#captcha_msg').show();
			}
		 });
	
	
}

function submitForm()
{
	if ( validationQuestion() )
	{
		$.post(baseurl+'/trivia.php', $("#question_form").serialize(), 
			function(data){
				//alert(data);	
				$('#reg_form_div').hide();
				$('#question_form_div').html(data);
				$('#question_form_div').show();
			}
		);
	}	
}


/*function loadQuestionForm()
{
	$.post(
	baseurl+"/trivia.php", 
	{action: "loadQuestionForm" }, 
	function(data)
	{
		$('#question_form_div').html(data);
	});
	
}*/

function validateAnswersForm()
{
	var error_msg = '';
	var questions = document.getElementsByName('questions[]');
	for( var i = 0; i < questions.length; i++ )
	{
		radio_val = $('input:radio[name=ans_'+questions[i].value+']:checked').val();
		var counter = (i+1) ; 
		if ( ! radio_val )
		{	
 			error_msg  = 'Please select the answer for question no. '+counter+'.';
			$('#div_error_msg_'+counter).show();
			$('#div_error_msg_'+counter).html(error_msg);
		}else{
			$('#div_error_msg_'+counter).hide();
		}
	}
	
	if(error_msg == '')
	{
		//$('#main_error_div').html('');	
		//$('#main_error_div').hide();
		return true;
	}
	else
	{
		//$('#main_error_div').html(error_msg);
		//$('#main_error_div').show();
		return false;
	}
}


function submitAnswers() //	 go to next step
{
	if( ! validateAnswersForm() )
	{
		return false;	
	}
	
	$("#question_form_div").hide();
	$("#reg_form_div").show();
	
	/*$.post(baseurl+'/trivia.php', $("#question_form").serialize(), 
		function(data){
			//alert(data);	
		}
	);
	*/
}



function checkCnicExist(){
	   
		var cnic = document.getElementById('cnic').value;
		 if( cnic == '' )
		{
			return '';	
		}
		else
		if( ! validCnicNo(cnic) )
		{
			return '';
		}	
			
			
		 $.post(
     	 baseurl+"/trivia.php", 
     	 {action: "check_cnic", cnic : cnic }, 
     	 function(data){
      		 
			if( data == 1)
			{
				$('#cnic_msg').show();
				$('#cnic_exist').val(0);
				$('#cnic').addClass('alert');
			}
			else
			{
				 $('#cnic_msg').hide();
				  $('#cnic_exist').val(1);
				  $('#cnic').removeClass('alert');
			}
		});
		  
}//end function
function validationQuestion(){
	
	     var error_msg = '';
		 var name = jQuery.trim(document.getElementById('name').value);
		 var email = jQuery.trim(document.getElementById('email').value);
		 var contact = jQuery.trim(document.getElementById('contact').value);
		 var city = jQuery.trim(document.getElementById('city').value);
		// var cnic = jQuery.trim(document.getElementById('cnic').value);
		 var captcha = jQuery.trim(document.getElementById('captcha').value);
		 if(name==''){
			error_msg += '<p>Please enter name.</p>';
		}
		
		if(name!=''  &&  (!ch_validUserName(name)) ){
						
				error_msg += '<p>Invalid characters, please enter valid characters in name.</p>';
					
		}
		if(email==''){
			error_msg += '<p>Please enter email.</p>';
		}
		if(email!=''  &&  (!ch_validEmail(email))){
				error_msg += '<p>Invalid email, please enter valid email.</p>';	
		}
		if(contact==''){
			error_msg += '<p>Please enter contact number.</p>';
		}
		if(contact!=''  &&  (!ch_validPhoneNumber(contact))){
			error_msg += '<p>Please enter valid contact number.</p>';
		}
		
		/*if(cnic==''){
			error_msg += '<p>Please enter cnic.</p>';
		}
		if(cnic!=''  &&  (!validCnicNo(cnic))){
				error_msg += '<p>Invalid cnic no, please enter valid cnic e.g : (12345-1234567-1).</p>';	
		}
		else
		if( $('#cnic_exist').val() == 0)
		{
			error_msg += '<p>CNIC number is already in use.</p>';
		}*/
		
		if(captcha==''){
			error_msg += '<p>Please enter captcha.</p>';
		}
		else
		if( $('#valid_captcha').val() == 0)
		{
			error_msg += '<p>Please enter valid captcha.</p>';
		}
		if(error_msg == '')
		{
			$('#main_error_div').html('');	
			$('#main_error_div').hide();
			return true;
		}
		else
		{
			var html_string = '<div class="error">'+error_msg+'</div>';	
			$('#main_error_div').html(html_string);
			$('#main_error_div').show();
			return false;
		}

}//end function 
function ch_validEmail(str)
{
 	var pattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
	if( pattern.test(str) )
		return true;
	else
		return false;
}
function ch_validUserName(str)
{
 	var pattern = /^([a-zA-Z0-9\.\s])+$/; 
	if( pattern.test(str) )
		return true;
	else
		return false;
}
function ch_validAddress(str)
{
	var pattern = /^([a-zA-Z0-9\s\,\#\:\.\/;-])+$/; 
	if( pattern.test(str) )
		return true;
	else
		return false;
}
function ch_validText(str)
{
	var pattern = /^([a-zA-Z0-9\\\s-\,\/\.'"\:&;\)\(\?])+$/; 
	if( pattern.test(str) )
		return true;
	else
		return false;
} 
function ch_validPhoneNumber(str)
{
	var pattern = /^([0-9\s\(\)\+-\/#])+$/; 
	if( pattern.test(str) )
		return true;
	else
		return false;
}
function validCnicNo(num){
		 var str_len =(num);
		 var pattern = /^([0-9\s\-])+$/; 
		 var string_arr = str_len.split("-",3);
		 var string_arr1 = string_arr[0];
		 var string_arr2 = string_arr[1];
		 var string_arr3 = string_arr[2];
		 if(pattern.test(num) ){
			 if(string_arr1!=undefined && string_arr2!=undefined &&  string_arr3!=undefined){
		 	 		var nic_str = string_arr1+'-'+string_arr2+'-'+string_arr3;
			 }else{
				  return false; 
			 }
		 }
		 if(str_len.length!=15){
				return false;
		 }
		 if(!str_len.match(nic_str)){
			  return false;
		 }else{ 
			  return true;	
		 }
 }
 
 
 
/*function validCnicNo(num){
		 var str_len =(num);
		 var string_arr = str_len.split("-",3);
		 string_arr1 = string_arr[0];
		 string_arr2 = string_arr[1];
		 string_arr3 = string_arr[2];
		 var nic_str = string_arr1+'-'+string_arr2+'-'+string_arr3;
		 if(str_len.length!=15){
				return false;
		 }
		 if(!str_len.match(nic_str)){
			   return false;
		 }else{
			  return true;	
		 }
		       
 }*/